
/**
 * name:        个人中心--商品评价
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */
$(function(){
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        // 是否登录
        strs=$.cookie('user').split(",")
        var token=''
        for(var i in strs){
            token=strs[6]
        }
        var url=window.location.href; 
        var order=$.cookie('mem').split('|') 
        
        var list=''
        list+='<li>'
        list+='<input type="hidden" name="score" value="5">'
        list+='<input type="hidden" class="orderId" name="orderId" value="">'
        list+='<div class="goods_evaluate_tit">'
        list+='<p class="e1">订单号：<span>9000000000123201</span></p>'
        list+='<p class="e2">2017-04-01 12:00:00</p>'
        list+='<div class="e3">'
        list+='<i class="active r1" data-score="差评" data-eval="1"></i>'
        list+='<i class="active r1" data-score="中评" data-eval="2"></i>'
        list+='<i class="active r1" data-score="中评" data-eval="3"></i>'
        list+='<i class="active r1" data-score="好评" data-eval="4"></i>'
        list+='<i class="active r1" data-score="好评" data-eval="5"></i>'
        list+='</div>'
        list+='<p class="e4">好评！</p>'
        list+='</div>'
        list+='<div class="goods_evaluate_cont">'
        list+='<div class="goods_evaluate_info">'
        list+='<a href="" class="goods_det">'
        list+='<input type="hidden" class="goodsId" name="goodsId" value="">'
        list+='<img src="../../img/goods/goods_09.jpg" class="goods_img" alt="">'
        list+='<p class="e5 goods_name">测试数据</p>'
        list+='<p class="e6 goodsSpec">测试数据/测试数据</p>'
        list+='<p class="e6 unitPrice">测试数据</p>'
        list+='<p class="e6 num">测试数据</p>'
        list+='</a>'
        list+='</div>'
        list+='<div class="goods_evaluate_txt">'
        list+='<input class="text" type="text"  placeholder="对商品是否满意，快来写下你的评论吧!">'
        list+='<textarea class="content" name="content"></textarea>'
        list+='</div>'
        list+='</div>'
        list+='</li>'
        
        var goods_det=document.getElementsByClassName('goods_det')
        var orderId=document.getElementsByClassName('orderId')
        var goodsimg=document.getElementsByClassName('goods_img')
        var name=document.getElementsByClassName('goods_name')
        var goodsSpec=document.getElementsByClassName('goodsSpec')
        var unitPrice=document.getElementsByClassName('unitPrice')
        var buyNum=document.getElementsByClassName('num')
        var orderNo=document.getElementsByClassName('order_no')
        var createTime=document.getElementsByClassName('time')
        var goodsId=document.getElementsByClassName('goodsId')
        var order2=order[0].split(',') 
        $('.orderId').val(order2[0])
        // 循环显示带评价商品
        for(var i=0; i<(order.length)-1; i++){ 
            $('.goods_evaluate_list ul').append(list)
            var order1=order[i].split(',')
            goods_det[i].href='/goods/goods_detail.html?goodsId='+order1[8]
            orderId[i].value=order1[0]
            goodsimg[i].src=order1[1]
            name[i].innerHTML=order1[2]
            goodsSpec[i].innerHTML=order1[3]
            unitPrice[i].innerHTML='￥'+order1[4]
            buyNum[i].innerHTML='x'+'  '+order1[5]
            orderNo.innerHTML=order1[6]
            createTime.innerHTML=order1[7]
            goodsId[i].value=order1[8]
        }
        //商品列表数量
        // if(goods_det.length>'1'){
        //     var length=goods_det.length
        //     var height=117*length+length-1
        //     console.log(height)
        //     $('.goods_evaluate_txt, .text, .goods_evaluate_cont .goods_evaluate_txt textarea').css('height','110px')  
        //     $('.goods_evaluate_cont .goods_evaluate_info a').css({
        //         'border-bottom':'solid 1px #e5e5e5url+'//     })  
        //     $('.goods_evaluate_cont .goods_evaluate_info a').last().css('border','0')
        //     console.log()
        //     $('.goods_evaluate_txt').css('margin-top',(height-150)/2)
        // }
        //商品评价
        // hover星星图标显示分数
        $('.ser2').each(function(index,t){
            $(t).find('i').hover(function(){
                var data_eval=$(this).attr('data-eval')
                $(t).find('i').removeClass('active')
                for(var i=0; i<data_eval; i++){
                    $(t).find('i').eq(i).addClass('active')
                }
                $(t).parents('.ser1').find('.f').text(data_eval+'分')
                $(t).parents('.ser1').find('input').val(data_eval)
            })
        })
        var contentList=''
        var score=''
        var goodsid=''
        var content=''  
        var isAnonymous=''
        // 是否显示名称评价
        $('.radio').find('input').eq(0).click()
        $('input[name="isAnonymous"]').click(function(){
            if($('input[name="isAnonymous"]').is(':checked')){
                isAnonymous=$('input:radio[name="isAnonymous"]:checked').val(); 
            }
        }) 
        // 评价商品评分
        $('.goods_evaluate_cont').each(function(index,t){
            $(t).parents('li').find('i').hover(function(){
                var data_eval=$(this).attr('data-eval')
                var data_score=$(this).attr('data-score')
                //商品评分
                $(t).parents('li').find('input[name="score"]').val(data_eval)
                $(t).parents('li').find('.goods_evaluate_tit').find('.e4').text(data_score+'!')
                $(t).parents('li').find('.goods_evaluate_tit .e3 i').removeClass('active')
                for(var i=0; i<data_eval; i++){
                    $(t).parents('li').find('i').eq(i).addClass('active')
                }           
            })  
            // 获取输入焦点的时候input输入框隐藏 多行文本框出现
            $(t).find('.text').focus(function(){
                $(t).find('.text').hide();
                $(t).find('.content').show().focus();
            })
            // 、失去焦点的时候 跟上面相反
            $(t).find('.content').blur(function(){
                if($(t).find('.content').val()==''){
                    $(t).find('.content').hide();
                    $(t).find('.text').show();
                }
            }) 
        })     
        $('.button').click(function(){
            var serviceScore=$('input[name="serviceScore"]').val()
            var sendScore=$('input[name="sendScore"]').val()
            var orderId=$('.orderId').val()
            $('.goods_evaluate_cont').each(function(index,t){
                score=$(t).parents('li').find('input[name="score"]').val()
                goodsid=$(t).find('.goodsId').val()
                content=$(t).find('.content').val()
                // 评价信息拼接成list格式
                contentList+='{'+'"goodsId":'+goodsid+','+'"score":'+score+','+'"content":'+'"'+content+'"'+'}'+','
            }) 
            var contentList2='['+contentList+']'
            if (contentList2.length > 0) {
                contentList2 = contentList2.substring(0, contentList2.lastIndexOf(','));
            }
            contentList3=contentList2+']' 
            $.ajax({
                url:new_url+'shop/member_evaluate/evaluate',
                data:{
                    token:token,
                    orderId:orderId,
                    serviceScore:serviceScore,
                    sendScore:sendScore,
                    isAnonymous:$('input:radio[name="isAnonymous"]:checked').val(),
                    contentList:contentList3,
                },
                dataType:'json',
                type:'post',
                success:function(e){ 
                    if(e.code=='1005'){
                        $.cookie('user','',{path:'/'});
                        window.location.href='/src/login/login.html?type=1'
                    }
                    if(e.code=='0'){
                        tc(e.message)
                        setTimeout(function(){
                            window.location.href='/src/member/member_order_detail.html?orderId='+orderId
                        },1000)
                    }
                    else{
                        tc(e.message)
                    }
                }
            })
        })
    
    }
})
